package sys.Flight_Information_Sysytem;

import sys.Flight_Information_Sysytem.bean.AirInfo;
import sys.Flight_Information_Sysytem.dao.AirInfoDao;
import sys.Flight_Information_Sysytem.dao.impl.AirInfoDaoImpl;

import java.text.ParseException;
import java.util.List;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) throws ParseException {
        int num = 0;
        AirInfoDao airInfoDao = new AirInfoDaoImpl();
        do {
            //1.菜单
            System.out.println("*********欢迎使用航班信息管理系统*********");
            System.out.println("1.查询所有航班\t2.按时间查询航班信息\t3.按目的地查询航班信息");
            System.out.println("4.删除航班\t5.更新航班信息\t6.离开程序");
            System.out.println("请输入选择:");
            Scanner input = new Scanner(System.in);
            num = input.nextInt();
            switch (num){
                case 1:
                    List<AirInfo> infos = airInfoDao.findAll();
                    System.out.println("编号\t航班号\t目的地\t起飞日期");
                    for (int i = 0;i<infos.size();i++) {
                        AirInfo airInfo = infos.get(i);
                        System.out.println((i+1)+"\t"+airInfo.getNumber()+"\t"+airInfo.getAddress()+"\t"+airInfo.getBeginTime());

                    }
                    break;
                case 2:
                    System.out.println("请输入起飞时间：");
                    String time = input.next();
                    List<AirInfo> infos1 = airInfoDao.findByTime(time);
                    System.out.println("编号\t航班号\t目的地\t起飞日期");
                    for (int i = 0;i<infos1.size();i++) {
                        AirInfo airInfo = infos1.get(i);
                        System.out.println((i+1)+"\t"+airInfo.getNumber()+"\t"+airInfo.getAddress()+"\t"+airInfo.getBeginTime());

                    }
                    break;
                case 3:
                    System.out.println("请输入目的地：");
                    String address = input.next();
                    List<AirInfo> infos2 = airInfoDao.findByAddress(address);
                    System.out.println("编号\t航班号\t目的地\t起飞日期");
                    for (int i = 0;i<infos2.size();i++) {
                        AirInfo airInfo = infos2.get(i);
                        System.out.println((i+1)+"\t"+airInfo.getNumber()+"\t"+airInfo.getAddress()+"\t"+airInfo.getBeginTime());

                    }
                    break;
                case 4:
                    System.out.println("请输入航班编号：");
                    String airNum = input.next();
                    int count= airInfoDao.delete(airNum);
                    System.out.println(count>0?"删除成功":"删除失败");
                    break;
                case 5:
                    System.out.println("请输入需要更新的航班的编号：");
                    String airNumber = input.next();
                    AirInfo buNumber = airInfoDao.findByNumber(airNumber);
                    if (buNumber!= null){
                        System.out.println("请输入新的航班编号：");
                        String airNewNumber = input.next();
                        System.out.println("请输入新的目的地：");
                        String newAddress = input.next();
                        System.out.println("请输入新的起飞时间：");
                        String newBeginTime = input.next();
                        AirInfo airInfo = new AirInfo();
                        airInfo.setNumber(airNewNumber);
                        airInfo.setAddress(newAddress);
                        airInfoDao.update(airInfo,airNumber,newBeginTime);
                    }else {
                        System.out.println("航班不存在");
                    }
                    break;
                case 6:
                    break;
                default:
                    System.out.println("请输入1-6的数字：");
            }
        }while (num != 6);
        System.out.println("谢谢使用！");
    }
}
